package com.umeox.ota.data.repository;

import android.app.Application;
import androidx.annotation.NonNull;

import com.umeox.ota.data.repository.cache.CacheType;
import com.umeox.ota.data.repository.cache.LruCache;
import com.umeox.ota.utils.DataHelper;
import com.umeox.ota.data.db.DatabaseModule;
import com.umeox.ota.data.repository.cache.Cache;

import java.io.File;

/**
 * RepositoryConfigModule
 */
public class RepositoryConfigModule {
    private Application mApplication;
    private File mCacheFile;
    private DatabaseModule.RoomConfiguration mRoomConfiguration;
    private Cache.Factory mCacheFactory;


    private RepositoryConfigModule(Builder builder) {
        this.mApplication = builder.application;
        this.mCacheFile = builder.cacheFile;
        this.mRoomConfiguration = builder.roomConfiguration;
        this.mCacheFactory = builder.cacheFactory;
    }

    public static Builder builder() {
        return new Builder();
    }

    File provideCacheFile() {
        //提供缓存文件
        return mCacheFile == null ? DataHelper.getCacheFile(mApplication) : mCacheFile;
    }

    DatabaseModule.RoomConfiguration provideRoomConfiguration() {
        return mRoomConfiguration == null ? DatabaseModule.RoomConfiguration.EMPTY : mRoomConfiguration;
    }

    Cache.Factory provideCacheFactory() {
        return mCacheFactory == null ? new Cache.Factory() {
            @NonNull
            @Override
            public Cache build(CacheType type) {
                //若想自定义 LruCache 的 size,或者不想使用 LruCache ,想使用自己自定义的策略
                //请使用 RepositoryConfigModule.Builder#cacheFactory() 扩展
                switch (type) {
                    case EXTRAS_CACHE_TYPE:
                        //外部 extras 默认最多只能缓存500个内容
                        return new LruCache(500);
                    default:
                        //RepositoryManager 中的容器默认缓存 100 个内容
                        return new LruCache(Cache.Factory.DEFAULT_CACHE_SIZE);
                }
            }
        } : mCacheFactory;
    }

    public static final class Builder {
        private Application application;
        private File cacheFile;
        private DatabaseModule.RoomConfiguration roomConfiguration;
        private Cache.Factory cacheFactory;


        private Builder() {
        }

        @NonNull
        public Builder application(Application application) {
            this.application = application;
            return this;
        }

        public Builder cacheFile(File cacheFile) {
            this.cacheFile = cacheFile;
            return this;
        }

        public Builder roomConfiguration(DatabaseModule.RoomConfiguration roomConfiguration) {
            this.roomConfiguration = roomConfiguration;
            return this;
        }

        public Builder cacheFactory(Cache.Factory cacheFactory) {
            this.cacheFactory = cacheFactory;
            return this;
        }


        public RepositoryConfigModule build() {
            return new RepositoryConfigModule(this);
        }

    }
}
